﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.PHighLight;
using Bussiness.PHighLight;
using Bussiness.Language;
using FOModels.Language;
using FOSystem.CultureHelper;
namespace FO.Controllers
{
    public class PHighLightController : CultureController
    {
        //
        // GET: /PHighLight/

        public ActionResult Index()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerPHighLight()
        {
            return View();
        }
        public JsonResult getPHighLightJson()
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PHighLightBLL cardbll = new PHighLightBLL();
            List<PHighLightModels> list = new List<PHighLightModels>();
            list = cardbll.getall(lang.LangID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPHighLight()
        {

            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertPHighLight(FO.Models.PHighLightInsert card)
        {
            PHighLightBLL cardbll = new PHighLightBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            if (ModelState.IsValid)
            {
                PHighLightModels items = new PHighLightModels();
                items.PHighLightName = card.PHighLightName;
                items.Display = false;
                items.OrderSort = card.OrderSort;
                items.Link = card.Link;
                cardbll.InsertPHighLight(items);

                PHighLightModels faqitems = cardbll.gettop1();
                PHighLightModels itemsLang = new PHighLightModels();

                items.PHighLightID = faqitems.PHighLightID;
                itemsLang.PHighLightID = faqitems.PHighLightID;
                itemsLang.PHighLightName = card.PHighLightName;
                itemsLang.PHighLightContent = card.PHighLightContent;
                cardbll.InsertPHighLightLang(itemsLang, 1);
                itemsLang.PHighLightName = card.PHighLightNameLang;
                itemsLang.PHighLightContent = card.PHighLightContentLang;
                cardbll.InsertPHighLightLang(itemsLang, 2);
                return RedirectToAction("ManagerPHighLight");
            }
            return View(card);

        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePHighLight(int PHighLightID)
        {
            PHighLightBLL cardbll = new PHighLightBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PHighLightModels card = new PHighLightModels();
            card = cardbll.getbyid(PHighLightID, lang.LangID);
            Models.PHighLight c = new Models.PHighLight();
            c.PHighLightName = card.PHighLightName;
            c.PHighLightContent = card.PHighLightContent;
            c.Link = card.Link;
            c.OrderSort = card.OrderSort;
            c.Display = card.Display;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdatePHighLight(FO.Models.PHighLight card)
        {
            PHighLightBLL cardbll = new PHighLightBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            PHighLightModels ca = new PHighLightModels();
            ca = cardbll.getbyid(card.PHighLightID, lang.LangID);
            if (ca != null)
            {
                ca.PHighLightName = card.PHighLightName;
                ca.PHighLightContent = card.PHighLightContent;
                ca.Link = card.Link;
                ca.OrderSort = card.OrderSort;
                ca.Display = card.Display;
                cardbll.UpdatePHighLight(ca, lang.LangID);
                return RedirectToAction("ManagerPHighLight");
            }
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeletePHighLight(int PHighLightID)
        {
            PHighLightBLL bll = new PHighLightBLL();
            LanguageBLL langbll = new LanguageBLL();
            PHighLightModels card = new PHighLightModels();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            card = bll.getbyid(PHighLightID,lang.LangID);
            if (card != null)
            {
                bll.DeletePHighLight(card.PHighLightID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
